//
//  ViewController.m
//  07-掌握-corelocation框架的基本使用—(反)地理编码
//
//  Created by J on 16/2/27.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *latitude;
@property (weak, nonatomic) IBOutlet UITextField *longtitude;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property(nonatomic,strong)  CLGeocoder* coder;
@end

@implementation ViewController
- (IBAction)GeoCode:(UIButton *)sender {
   
  
    
    
    [self.coder geocodeAddressString:self.textView.text.length?self.textView.text:@"" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
       
        if(!error){
            
            [placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull mark, NSUInteger idx, BOOL * _Nonnull stop) {
                self.latitude.text = @(mark.location.coordinate.latitude).stringValue;
                self.longtitude.text = @(mark.location.coordinate.longitude).stringValue;
                

            }];

        }
        
    }];
    
}
- (IBAction)reverseGeoCode:(UIButton *)sender {
    
 
    CLLocation * local = [[CLLocation alloc]initWithLatitude:self.latitude.text.doubleValue longitude:self.longtitude.text.doubleValue];
    [self.coder reverseGeocodeLocation:local completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if(!error){
            [placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull mark, NSUInteger idx, BOOL * _Nonnull stop) {
                self.textView.text = mark.thoroughfare;
   
            }];
            
            
        }
        
        
    }];
    

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    CLGeocoder* coder = [[CLGeocoder alloc]init];
    self.coder = coder;
    
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
